import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {PERMISSION} from './constants';
import {FORM_OPTIONS} from '../constants';
import {hasPermissions} from './utils';
import {injectIntl} from './intl';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import TextInput from 'Shared/components/TextInput';
import Export from '../common/Export';
import ImportPanel from './ImportPanel';
const FormItem = Form.Item;
class QueryPanel extends React.PureComponent {
    state = {
        showImport: false
    }
    handleFilterChange = (value, name) => {
        this.props.onChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onChange(name, value);
        this.props.querySearch();
    }
    onClickImport = () => this.setState({showImport: true});
    onCloseImport = () => this.setState({showImport: false});
    render() {
        const operations = [];
        const {formatMessage} = this.props.intl;
        if(hasPermissions(this.props.permissions, PERMISSION.import))
            operations.push(<Button key="import" type="primary" onClick={this.onClickImport}>
                {formatMessage({
                    id: 'queryPanel.btn.import',
                    defaultMessage: '导入'
                })}</Button>);
        const dealerInfo = this.props.condition.dealerId ? {
            id: this.props.condition.dealerId,
            code: this.props.condition.dealerCode,
            name: this.props.condition.dealerName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        {this.props.isDealerUser && <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    value={dealerInfo}
                                    onSelect={this.props.onSelect} />
                            </FormItem>
                        </Col>}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.year',
                                    defaultMessage: '年份'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="year"
                                    value={this.props.condition.year}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.month',
                                    defaultMessage: '月份'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="month"
                                    value={this.props.condition.month}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary"
                                onClick={this.props.querySearch} loading={this.props.loading}>
                                {formatMessage({
                                    id: 'queryPanel.btn.search',
                                    defaultMessage: '查询'
                                })}</Button>
                            <Button key="clear" onClick={this.props.onClickReset}>{formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            {hasPermissions(this.props.permissions, PERMISSION.export) &&
                                <Export key={PERMISSION.export} exportRequest={this.props.exportData} />
                            }
                            {operations}
                        </Col>
                    </Row>
                </Form>
                {this.state.showImport &&
                    <ImportPanel
                        selectKey="showImport"
                        onCancel={this.onCloseImport}
                        onImportChange={this.onImportChange} />
                }
            </div>
        );
    }
}

QueryPanel.propTypes = {
    condition: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onChange: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    isDealerUser: PropTypes.bool
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    querySearch,
    resetQueryPanel,
    selectDealerQuery,
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    condition: getConditions(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealerQuery(data)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
